import { IData } from '@amcax/base';

/**
 * 文档信息
 */
export type IDocument = {
  name: string;
  collectionUuid: string; // 项目uuid
  documentId: string; // 文档uuid，后端返回的
  uuid: string; // 同documentId，历史问题，前端使用的是uuid，在获取文档信息时赋值
  version: string; // 文档版本号
  readonly: boolean; // 文档是否只读
  description: string;
  needUpdate: boolean;
  geometries: IComponent[]; // component列表
  collections: ICollection[]; // 集合列表
  meshControl?: IMeshControl; // 网格控制参数，包括：MeshSize、BoundaryLayer、Distribution，取代preprocess
  simulationPlugins?: ISimulationPlugin[]; // 仿真支持的插件列表
  simulations: any[]; // 仿真列表
  tasks?: ITask[]; // 求解（任务）列表
  results?: IResult[]; // 结果列表
  postprocesses: any[]; // 后处理（可视化）列表

  // 其他缓存数据
  geometriesId?: Set<string>; // 几何id
  geometriesData?: any[]; // 几何渲染数据
  meshesId?: Set<string>; // 网格id
  meshesData?: IData[]; // 网格数据
};

/**
 * Component信息（现在存放在Document的geometries中
 */
export type IComponent = {
  uuid?: string;
  name?: string;
  type?: string; // nextmesh
  color?: string; // 几何/网格的颜色
  geometryVisible?: boolean; // 几何是否可见
  meshVisible?: boolean; // 网格是否可见
  objects?: string[]; // 管理的对象id，应该要区分几何/网格
  description?: string;
  createdAt?: string;
  updatedAt?: string;
};
/**
 * 网格控制信息
 */
export type IMeshControl = {
  params: {
    BoundaryLayer: IMeshBoundaryLayer[];
    Distribution: IMeshDistribution[];
    MeshSize: IMeshSize[];
  };
};

export type IMeshControlBase = {
  SelectedEntities: string[];
  Name?: string;
  isGlobal?: boolean;
};

/**
 * 网格尺寸
 */
export type IMeshSize = {
  MaxSize?: number;
  MinSize?: number;
  GrowthRate?: number;
  CurvatureFactor?: number;
  MeshingDim?: number; // 网格化维度
} & IMeshControlBase;

/**
 * 网格分布
 */
export type IMeshDistribution = {
  EntityType: 'Edge';
  DistributionType: 'Separation';
  DistributionPara: number;
} & IMeshControlBase;

/**
 * 网格边界层
 */
export type IMeshBoundaryLayer = {
  EntityType: 'Edge';
  GrowthRate: number;
  InitLayerHeight: number; // (0, inf]
  NLayers: number; // 总层高 整形 [1, inf]
} & IMeshControlBase;

/**
 * 网格种子
 */
export type IMeshSeed = {
  Method: string; // 方式：BySize/ByNumber
  Bias: string; // 偏斜：None/Linear/Exponential/BellCurve
  Size?: number;
  MaxSize?: number;
  MinSize?: number;
  Number?: number;
  BiasValue?: number;
} & IMeshControlBase;
/**
 * 集合信息
 */
export type ICollection = {
  Name: string;
  Type: 'Node' | 'Element' | 'Surface' | 'ReferPoint' | undefined;
  EntityType?: string;
  SelectedEntities: string[];
};

/**
 * 仿真支持的插件信息
 */
export type ISimulationPlugin = {
  name: string;
  support: string[];
};

/**
 * 仿真信息
 */
export type ISimulation = {
  uuid?: string;
  name: string;
  simuType: string; // 仿真类型
  simuPlugin: string; // 仿真插件
  condition?: any;
  createdAt?: string; // '2023-10-23T09:12:51'
  updatedAt?: string; // '2023-10-23T09:12:51'
};

/**
 * 任务信息
 */
export type ITask = {
  uuid: string;
  name: string;
  type: 'mesh' | 'simulation'; // 任务类型：mesh/simulation
  moduleId: string; // 对应的mesh/simulation id
  status:
    | 'CREATED'
    | 'STARTED'
    | 'CANCELING'
    | 'CANCELED'
    | 'FAILED'
    | 'FINISHED';
  progress: number; // 任务进度最小0，最大1
  docid?: string;
  docver?: string;
  plugin?: string;
  started?: string; // '2023-10-23T09:02:51'
  finished?: string; // '2023-10-23T09:12:51'
  createdAt?: string; // '2023-10-23T09:12:51'
  updatedAt?: string; // '2023-10-23T09:12:51'
};

export type ISolveFrame = {
  frame: number;
};

/**
 * 字段信息
 */
export type IField = {
  name: string;
  type: 'scalar' | 'vector' | 'tensor';
  Variable?: string[];
  layer?: string[];
};

/**
 * 求解结果信息
 */
export type ISolve = {
  step: number;
  fieldNames: string[];
  frames?: ISolveFrame[]; // 每一帧的求解信息
};

/**
 * 求解结果信息
 */
export type ISolveNew = {
  step: number;
  field?: IField[];
  frames?: object[]; // 每一帧的求解信息
};

/**
 * 结果信息
 */
export type IResult = {
  uuid: string;
  name: string;
  condition?: any;
  fieldNames?: string[]; // 场名列表
  field?: string[]; // 场名列表
  solve: ISolve[]; // 求解结果
};

/**
 * 结果信息
 */
export type IResultNew = {
  uuid: string;
  name: string;
  condition?: any;
  fieldNames?: string[]; // 场名列表
  solve: ISolveNew[]; // 求解结果
};

export enum GspEventType {
  taskupdated = 'taskupdated',
  taskmonitor = 'taskmonitor',
  shapeChanged = 'shapeChanged',
  meshChanged = 'meshChanged',
}

export interface IGspEvent {
  event: GspEventType;
  uuid?: string; // uuid
  data?: any;
}
